/*
 * #%L
 * SwiftUIBusinessLayer
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.plugins;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * @author ashwin kumar
 *
 */
public class EventServiceBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
	String extensionBeanName;
	String propertyName;
	String pluginBeanName;
	
	static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(EventServiceBeanFactoryPostProcessor.class);
	
	/**
	 * The bean that is being extended (the bean with a {@link List} based
	 * property.
	 * @param beanName Spring bean name.
	 */
	public void setExtensionBeanName(String beanName) {
		this.extensionBeanName = beanName;
	}

	/**
	 * The name of the {@link List} property within the
	 * {@link #setExtensionBeanName(String) extension} bean.
	 * @param propertyName property name.
	 */
	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
	
	/**
	 * The name of the bean to plug-in to the extension bean's list property.
	 * @param pluginName The plugin bean's name.
	 */
	public void setPluginBeanName(String pluginName) {
		pluginBeanName = pluginName;
	}

	////////////////////////////////////////////////////////////////////////////
	// BeanFactoryPostProcessor method
	/*
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
	 */
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
			throws BeansException {
		System.out.println("In Post Processor");
		for(String beanName:beanFactory.getBeanDefinitionNames()){
			System.out.println("Child Factory has Bean with name:"+beanName);
			
			if(StringUtils.endsWithIgnoreCase(beanName, "Plugin")){
				PluginInstanceHolder.getInstance().storePluginRefernce(beanName, beanFactory.getBean(beanName, AbstractSwiftUIPlugin.class));
			}
		}
		
		//beanFactory.getParentBeanFactory().containsBean("platformService")
		//beanFactory.containsBeanDefinition("platformService")
		/*logger.info("Plugging in " + pluginBeanName + " into bean:" + extensionBeanName + " property:" + propertyName);
		if ( extensionBeanName == null || !beanFactory.containsBeanDefinition(extensionBeanName) )
			throw new IllegalArgumentException("Cannot find bean " + extensionBeanName);
		
		BeanDefinition beanDef = beanFactory.getBeanDefinition(extensionBeanName);
		MutablePropertyValues propValues = beanDef.getPropertyValues();
		if ( propertyName == null || !propValues.contains(propertyName))
			throw new IllegalArgumentException("Cannot find property " + propertyName + " in bean " + extensionBeanName);
		PropertyValue pv = propValues.getPropertyValue(propertyName);
		Object prop = pv.getValue();
		if ( !(prop instanceof List))
			throw new IllegalArgumentException("Property " + propertyName +
					                           " in extension bean " + extensionBeanName +
					                           " is not an instanceof List."); 
			
		List l = (List) pv.getValue();
		
		l.add(new RuntimeBeanReference(pluginBeanName));*/
	}
}
